#!/usr/bin/env php
<?php
namespace workerbase\cli;
/**
 * 使用linux的crontab配置定时任务
 * @author fukaiyao
 */

// fix for fcgi
defined('STDIN') or define('STDIN', fopen('php://stdin', 'r'));

//定义项目根目录
define('WORKER_PROJECT_PATH',  __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);

require_once WORKER_PROJECT_PATH.'workerbase/Load.php';

date_default_timezone_set('PRC');
//初始化配置参数
//php命令
$phpPath = loadc('config')->get("phpbin");
//项目目录
$projectPath = WORKER_PROJECT_PATH;
//进程运行角色
$uid = loadc('config')->get("user", "cron");

//定制任务配置
//# Example of job definition:
//# .---------------- minute (0 - 59)
//# |  .------------- hour (0 - 23)
//# |  |  .---------- day of month (1 - 31)
//# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
//# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
//# |  |  |  |  |
//# *  *  *  *  * user-name  command to be executed

$options = getopt('d');
if (isset($options['d'])) { //清空定时任务
    $crontab = '';
} else {
$crontab = <<< EOT
#每分钟尝试启动一次swoole定时任务和worker队列服务
*/1 * * * * {$uid} sh {$projectPath}scripts/server.sh start
EOT;
}

//安装定时任务
loadf('installCrontab');
